#ifndef _CRT_SECURE_NO_WARNINGS
	#define _CRT_SECURE_NO_WARNINGS // Deal with warnings in glui.h
#endif

#include <GL\glui.h>
#include "Animation.h"
#include "Objects.h"
#include "textures.h"
#include "Functions.h"


int currentState = PRINTING;


float robotTrajectory[ROBOT_MAX_STATE][3] = {
	{ROBOT_X, ROBOT_Y, ROBOT_Z									 },
	{DOOR_X + DOOR_DIM_X/2.0,  ROBOT_Y, ROBOT_Z					 },
	{DOOR_X + DOOR_DIM_X/2.0,  ROBOT_Y, DOOR_Z + ROBOT_DIM_X/2.0 },
	{DOOR_X + DOOR_DIM_X/2.0,  ROBOT_Y, ROBOT_Z					 },
	{ROBOT_X, ROBOT_Y, ROBOT_Z									 }
};


int count = 0;
int robotSteps = 100;
int robotState_index = ROBOT_STATE1;
float robot_delta[ROBOT_MAX_STATE-1][3];
float robot_posx, robot_posz;

void initRobotDeltas() {
	for(int i = 0; i < ROBOT_MAX_STATE - 1; ++i) {
		robot_delta[i][0] = (robotTrajectory[i+1][0] - robotTrajectory[i][0]) / (float) robotSteps;
		robot_delta[i][2] = (robotTrajectory[i+1][2] - robotTrajectory[i][2]) / (float) robotSteps;
	}
}

void animatePrinter(float obj_rotate)
{
	glTranslated(+CYLINDER_X, +CYLINDER_Y, +CYLINDER_Z);
	glRotated(obj_rotate, 1,0,0);
	glTranslated(-CYLINDER_X, -CYLINDER_Y, -CYLINDER_Z);
}

void animateGuillotine(float guillotineStepY)
{
	glTranslated(0, guillotineStepY, 0);
}

void animateCompressor() {

}

void animateRobot() {

	//glPushMatrix();
	if( robotState_index == ROBOT_STATE2 || robotState_index == ROBOT_STATE3 ) {
		glTranslatef(ROBOT_X + robot_posx, 0.0, ROBOT_Z + robot_posz);
		glRotatef(90, 0.0, 1.0, 0.0);
		glTranslatef(-ROBOT_X - robot_posx, 0.0, -ROBOT_Z - robot_posz);
	}

	glTranslatef(robot_posx, 0.0, robot_posz);

	count++;

	if( count == robotSteps) {
		if( robotState_index == ROBOT_STATE4 ) {
			robotState_index = ROBOT_STATE1;
			currentState = PRINTING;
		}
		else robotState_index++;
		count = 0;
	}
	//glPopMatrix();
}

void handlePaper(Paper &paper, int curState, int it) {
	
	int state = paper.getState();
	
	printPaper(paper, it);

	if (curState == PAPER_WAIT) {
	}
	else if (state == PAPER_PRINTING) {

		if (paper.getDimX() >= PAPER_DIM) {
			paper.setDimX(PAPER_DIM);
			paper.setState(PAPER_ROLLING);
		}
	}
	else if (state == PAPER_ROLLING) {
		paper.setZ( paper.getZ() + PAPER_DIM_INC);

		if (paper.getZ() >= PAPER_DROP_Z) {
			paper.setState(PAPER_DROP);
		}
		else if (paper.getZ() >= PAPER_GUILL_Z && paper.getCut()==0) {
			paper.setState(PAPER_CUT);
		}

	}
	else if (state == PAPER_CUT) {
		paper.setZ( paper.getZ() + PAPER_DIM_INC);

		if (paper.getZ() >= PAPER_GUILL_Z + PAPER_DIM) {
			paper.setState(PAPER_ROLLING);
			paper.setCut(1);
		}
	}
	else if(state == PAPER_DROP) {
		paper.setZ( paper.getZ() + PAPER_DIM_INC); 
		if (paper.getZ() >= ROBOT_Z) {
			paper.setZ( ROBOT_Z);

			paper.setY( paper.getY() - (float)PAPER_DIM_INC/10);
			if (paper.getY() <= ROBOT_Y + it*ROBOT_DIM_Y/2/N_PAPERS) {
				//paper.setY( ROBOT_Y);
				paper.setState(PAPER_ROBOT);
			}
		}
	}
	else if (state == PAPER_ROBOT) {
		//glPushMatrix();
			//glScaled(1,ROBOT_DIM_Y*it/N_PAPERS , 1);
			//glTranslatef(0, it*ROBOT_DIM_Y/N_PAPERS , 0);
		//glPopMatrix();
	}
	else if (state == PAPER_WAIT) {
	}
}

void printPaper(Paper &paper, int i) {
	glPushMatrix();
			glTranslatef(paper.getX(), paper.getY() , paper.getZ());
			/*if (paper.getState() == PAPER_ROBOT ) 
				glTranslatef(0, i*ROBOT_DIM_Y/N_PAPERS , 0);/*/
			
			glScalef(paper.getDimX(), 1, paper.getDimZ());
			/*if (paper.getState() == PAPER_ROBOT )
				glScaled(1, i*ROBOT_DIM_Y/N_PAPERS , 1);*/
			glRotated(-90, 1, 0 ,0);
			drawRect(LETTERS_TEX_ID);
	glPopMatrix();
}